/**
 * 
 */
package com.nwpusp.model;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;

import com.nwpusp.controller.actions.Action;
import com.nwpusp.controller.actions.GameActionListener;
import com.nwpusp.display.GlobalVar;
import com.nwpusp.display.MainActivity;
import com.nwpusp.display.UsrEventHandler;
import com.nwpusp.model.players.*;
import com.nwpusp.model.session.*;

/**
 * @author JustinCao
 *
 */
public class GameModel implements GameActionListener {
	private PlayerManager pm = null;
	private Scenario sr = null;
	private SessionManager sm = null;
	private UsrEventHandler ueh = null;
	private Activity activity = null;

	public GameModel(Activity a) {
		sm = new SessionManager((WifiManager)a.getSystemService(Context.WIFI_SERVICE));
		pm = new PlayerManager(sm);
		sr = new Scenario(100 * 5000, 2000 * 6, 1500 * 4);
		activity = a;
	}
	
	public void setUsrEventHandler(UsrEventHandler ueh) {
		this.ueh = ueh;
	}
	public PlayerManager getPlayerManager() {
		return pm;
	}
	
	public Scenario getScenario() {
		return sr;
	}
	
	public SessionManager getSessionManager() {
		return sm;
	}
	
	@Override
	public void OnAction(ArrayList<Action> al) {
		for(Action a : al) {
			a.Invoke(this);
		}
	}
	
	public ArrayList<Entity> getDispEntities() {
		if(sr != null) {
			return sr.getDispEntities(pm.getLocalPlayer());
		}
		return null;
	}
	
	public void startAllGame() {
		if(ueh != null) {
			ueh.startGame(false);	
		}
	}
	
	public void startLocalGame() {
		if(ueh != null && activity != null) {
			// need
			ueh.startGame(true);
		}
	}
	
	public void stopLocalGame() {
		if(ueh != null) {
			ueh.stopGame(true);
		}
	}

	public void enterGameViewLocal() {
		((MainActivity)activity).mhadnler.sendEmptyMessage(GlobalVar.ENTER_GAMEVIEW);
	}
	
	public void allEnterGame() {
		if(ueh != null) {
			ueh.enterGame();
		}
	}

	public void Init() {
		sm.Init();
		pm.Init();
		sr.Init();
	}

	public void move2CreateView() {
		((MainActivity)activity).mhadnler.sendEmptyMessage(GlobalVar.BACK);
	}

	public void trackDirectionMove(int moveValue) {
		if(ueh != null) {
			ueh.trackDirectionMove(moveValue);
		}
	}

	public void trackShift(int direction) {
		if(ueh != null) {
			ueh.trackShift(direction);
		}
	}
	
	public void Response(ConnectionId monitorCid, int syncId) {
		if(ueh != null) {
			ueh.Response(monitorCid, syncId);
		}
	}

	public String getLocalPlayerName() {
		return ((MainActivity)activity).playerName;
	}

	public ConnectionId getServerConnectionId() {
		return getSessionManager().getServerConnectionId();
	}

	public void startRunningCounterTimer() {
		((MainActivity)activity).startRunningCounterTimer();
	}

	public void start3_2_1_GoTimer() {
		((MainActivity)activity).start3_2_1_GoTimer();
	}

	public void everybodyGo() {
		if(ueh != null) {
			ueh.everybodyGo();
		}
	}

	public void reachEndLocal() {
		if(ueh != null) {
			ueh.reachEnd();
		}
		
	}
}
